package com.mlnx.work.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mlnx.common.annotations.NeedLogin;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.enums.BusinessType;
import com.mlnx.common.form.PageForm;
import com.mlnx.work.config.ControllerNames;
import com.mlnx.work.config.RightKeys;
import com.mlnx.work.entity.Department;
import com.mlnx.work.pojo.form.DepartmentFilterForm;
import com.mlnx.work.pojo.form.DepartmentForm;
import com.mlnx.work.pojo.form.NoneFilterForm;
import com.mlnx.work.pojo.vo.SelfDepartmentVo;
import com.mlnx.work.service.DepartmentService;

import org.shan.spring.base.BaseController;
import org.shan.spring.base.annotations.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Created by amanda.shan on 2020/5/19.
 */
@SysLog(title = "用户权限")
@Api(value = ControllerNames.DepartmentName, tags = {ControllerNames.DepartmentName})
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "分页获取", notes = "")
    @GetMapping("")
    public ResponseData<IPage> listPage(@Valid PageForm pageForm, DepartmentFilterForm ft) throws IllegalAccessException {

        return result(departmentService.page(pageForm, ft));
    }

    @ApiOperation(value = "获取所有", notes = "")
    @GetMapping("/all")
    public ResponseData<List<Department>> listAll(NoneFilterForm ft) throws IllegalAccessException {

        return result(departmentService.list(ft));
    }

    @ApiOperation(value = "根据ID获取", notes = "")
    @GetMapping("/{id}")
    public ResponseData<Department> getById(@PathVariable("id") Integer id) {

        return result(departmentService.getById(id));
    }

    @NeedLogin()
    @ApiOperation(value = "获取自身的部门信息", notes = "")
    @GetMapping("/selfDept")
    public ResponseData<SelfDepartmentVo> getById() {

        return result(departmentService.getSelfDepartment());
    }


    @NeedLogin(permissions = {RightKeys.RIGHT_DEPT_MANAGER})
    @SysLog(businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增", notes = "")
    @PostMapping("")
    public Response add(@Valid DepartmentForm form) throws IllegalAccessException, InstantiationException {

        Department t = new Department();
        BeanUtil.copyProperties(form, t);
        departmentService.save(t);

        return result();
    }

    @NeedLogin(permissions = {RightKeys.RIGHT_DEPT_MANAGER})
    @SysLog(businessType = BusinessType.UPDATE)
    @ApiOperation(value = "更改", notes = "")
    @PutMapping("/{id}")
    public Response modify(@PathVariable("id") Integer id, @Valid DepartmentForm form) {

        Department t = new Department();
        t.setId(id);
        BeanUtil.copyProperties(form, t);

        departmentService.updateById(t);

        return result();
    }

    @NeedLogin(permissions = {RightKeys.RIGHT_DEPT_MANAGER})
    @SysLog(businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable("id") Integer id) {

        departmentService.removeById(id);

        return result();
    }

    @NeedLogin(permissions = {RightKeys.RIGHT_DEPT_MANAGER})
    @ApiOperation(value = "批量删除", notes = "")
    @DeleteMapping(value = "")
    public Response deletes(Integer[] ids) {

        departmentService.removeByIds(Arrays.asList(ids));

        return result();
    }

}
